﻿package controllers.portal;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rbac.models.Oplog;
import rbac.models.User;

import models.Info;
import models.Link;

import com.et.mvc.BinaryView;
import com.et.mvc.JsonView;
import com.et.mvc.MultipartFile;
import com.et.mvc.TextView;
import com.et.mvc.View;

import com.et.mvc.filter.BeforeFilter;
import com.et.mvc.filter.BeforeFilters;

import controllers.ApplicationController;


public class LinkController extends ApplicationController {
	
	public void index() throws Exception{
    	
	}
	
	public View getLinkItems(int page, int rows, String q,int t, String sort, String order) throws Exception {
		String orderBy = "id";
		if (sort != null && order != null){
			orderBy = sort + " " + order;
		}
		String cond = "1=1";
		List<Object> tmpArgs = new ArrayList<Object>();
		
		/**
		if (t != 0){
			cond += " and (parentId = ?)";
			tmpArgs.add(t);
		}
		**/
		if (q != null){
			cond += " and (name like '%"+q+"%' or sxname like '%"+q+"%' or url like '%"+q+"%' ) ";			
		}
		

		Object[] args = tmpArgs.toArray();
		
		long total = Link.count(Link.class, cond, args);
		List<Link> items = Link.findAll(Link.class, cond, args, orderBy, rows, (page-1)*rows);
		
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("total", total);
		result.put("rows", items);
		
		return new JsonView(result);
	}
	
	public View saveLink(Link item, String[] rids) throws Exception {
		Map<String,Object> result = new HashMap<String,Object>();
		User user = (User) rbac.getCurrentUser();	
		if (item.save()){
			Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")新建友情链接，内容为：" + new JsonView(item));
			
			result.put("success", true);
		} else {
			result.put("failure", true);
			result.put("msg", getErrorMsg(item));
		}
		
		View view = new JsonView(result);
		view.setContentType("text/plain;charset=utf-8");
		return view;
	}
	
	public View updateLink(int id, Map<String,Object> params, String[] rids) throws Exception {
		Map<String,Object> result = new HashMap<String,Object>();
		User user = (User) rbac.getCurrentUser();	
		Link item = Link.find(Link.class, id);
		JsonView olditem= new JsonView(item);
		item = Link.updateModel(item, params);
		if (item.save()){
			Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")修改链接，原内容为："+ olditem + "\r\n修改内容为：" + new JsonView(item));										

			result.put("success", true);
		} else {
			result.put("failure", true);
			result.put("msg", getErrorMsg(item));
		}
		
		View view = new JsonView(result);
		view.setContentType("text/plain;charset=utf-8");
		return view;
	}
	
	public View destroyLink(int id) throws Exception {
		Link item = Link.find(Link.class, id);
		User user = (User) rbac.getCurrentUser();				
		Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")删除友情链接，内容为：" + new JsonView(item));									

		item.destroy();
		return new JsonView("success:true");
	}
	
}
